package com.example.itshome.network;

import android.app.Activity;
import android.widget.ImageView;
import com.example.itshome.adapter.ShopPageRecycleViewAdapter;
import com.example.itshome.entity.CircleEntity;
import com.example.itshome.entity.CommodityEntity;
import com.example.itshome.manager.UserInfoManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GetCommodityRequest extends AbstractRecycleVIewGetRequest<CommodityEntity>{
    public static String CAT_CATEGORY="猫咪专区";
    public static String DOG_CATEGORY="狗狗专区";
    public static String OTHER_CATEGORY="其他动物";
    public static String ALL_CATEGORY="推荐";
    private String category;

    public GetCommodityRequest setCategory(String category) {
        this.category = category;
        return this;
    }

    public GetCommodityRequest(Activity activity) {
        super(activity);
    }

    @Override
    protected String getPathAndParams() {
        return "shop/get-ip?username="+ UserInfoManager.getInstance().getUser().getUsername()+"&prefecture="+category;
    }

    @Override
    protected void handleResult(String result) {
        try {
            List<CommodityEntity> commodityEntities=new ArrayList<>();
            JSONObject jsonObject=new JSONObject(result);
            int code=jsonObject.getInt("code");
            if(code!=200){
                return;
            }
            JSONArray data=jsonObject.getJSONArray("data");
            int len=data.length();
            for (int i=0;i<len;i++){
                JSONObject item=data.getJSONObject(i);
                CommodityEntity entity=new CommodityEntity()
                                .setTitle(item.getString("name"))
                                .setId(item.getInt("ipId"))
                                .setPrice(item.getString("price"))
                                .setCollection(item.getBoolean("isFavorite"))
                                .setSurfacePictureSrc(DOMAIN_HOST+ IMAGES+ item.getString("coverPath"))
                                .setInsidePictureSrc(DOMAIN_HOST+IMAGES+item.getString("coverPath"))
                                .setWatchCount(item.getString("views"))
                                .setFavoriteCount(item.getString("star"))
                                .setDescription(item.getString("info"));


                commodityEntities.add(entity);
            }
            setHandleResultRunnable(new Runnable() {
                @Override
                public void run() {
                    for(CommodityEntity entity:commodityEntities){
                        addItem(entity);
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
